/*
 * @Author: 黄承文 chengwen@ssc-hn.com
 * @Date: 2023-02-17 14:02:48
 * @LastEditors: xinqiao lxq1990.0716@qq.com
 * @LastEditTime: 2024-01-05 16:05:44
 * @FilePath: /sscd/src/router/index.ts
 * @Description: 路由配置
 */

import { App } from 'vue'
import { createRouter, RouteRecordRaw, createWebHashHistory } from 'vue-router'
import { loadDemo } from '@/service/packages'

const history = createWebHashHistory()

export const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Home',
    redirect: '/list',
    // component: () => import('@/views/home/index.vue'),
  },
  {
    path: '/list',
    name: 'List',
    component: () => import('@/views/list/index.vue'),
  },
  {
    path: '/doc/:name',
    name: 'Doc',
    component: () => import('@/views/doc/index.vue'),
  },
  {
    path: '/package/:name',
    name: 'Package',
    component: () => import('@/views/package/index.vue'),
  },
  {
    path: '/package-full/:name',
    name: 'PackageFull',
    component: () => import('@/views/package/index.vue'),
  },
  {
    path: '/preview/:name',
    name: 'Preview',
    component: () => import('@/views/preview/index.vue'),
  },
  {
    path: '/preview-list',
    name: 'PreviewList',
    component: () => import('@/views/list/phone.vue'),
  },
  {
    path: '/:path(.*)*',
    name: 'error404',
    component: () => import('@/views/error-page/404.vue'),
  },
]

const router = createRouter({
  history,
  routes,
  strict: true,
})

export async function setupRouter(app: App) {
  router.beforeEach(async (to, from, next) => {
    if (['Package', 'PackageFull', 'Preview'].includes(to.name as string)) {
      await loadDemo(to.params.name as string, app)
      next()
    } else {
      next()
    }
  })
  app.use(router)
}

export default router
